package com.xiaoq.coms.util;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

public class OrderByUtil {

	public static void main(String[] args) {
		String orderBy = "[{\"abc\": \"ASC\"},{\"xyz\": \"DESC\"}, {\"np\": \"DESC\"}]";
		Map<String, String> propMap = new HashMap<String, String>();
		propMap.put("abc", "ABC_X");
		propMap.put("xyz", "NXY_XYZ");
		propMap.put("np", "NN_NP");
		
		System.out.println(normalize(orderBy, propMap));
	}
	
	public static String normalize(String orderBy, Map<String, String> propMap)  {
		if (StringUtils.isBlank(orderBy)) {
			return "";
		}
		orderBy = StringUtils.remove(orderBy, "[");
		orderBy = StringUtils.remove(orderBy, "]");
		orderBy = StringUtils.remove(orderBy, " ");
		orderBy = StringUtils.remove(orderBy, "\t");
		orderBy = StringUtils.remove(orderBy, "\n");
		orderBy = StringUtils.remove(orderBy, "\r\n");
		for (String key : propMap.keySet()) {
			orderBy = StringUtils.replace(orderBy,"\"" + key + "\"", propMap.get(key));
		}
		orderBy = StringUtils.remove(orderBy, "\"");
		orderBy = StringUtils.replace(orderBy, ":", " ");
		orderBy = StringUtils.replace(orderBy, "},{", ", ");
		orderBy = StringUtils.remove(orderBy, "{");
		orderBy = StringUtils.remove(orderBy, "}");
		return orderBy;
	}

}
